import {
	apiBaseUrl,
	TOKENNAME
} from './config.js';
import * as common from './common.js' //引入common
import * as db from './db.js' //引入common
// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = [
	'task/create',
	'user/task/post',
	'user/task/lst',
	'task/combination',
	'user/task/receipt',
	'user/task/submit',
	'user/task/pay',
	'user/vip',
	'user/course',
	'user/coin',
	'user/bean',
	'user/contribution',
	'user/balance',
	'user/task/examine',
	'task/lst',
	'user/extract',
	'task/combination/give',
	'user/task/combination/receive',
	'user/combination/give',
	'store/product/hot/hot',
	'store/product/hot/best',
	'user/recharge',
	'user/address/create',
	'user/cart/create',
	'user/cart/count',
	'user',
	'user/extract/create',
	'user/extract/banklst',
	'user/spread_info',
	'user/bind/douyin',
	'user/bind/kuaishou',
	'user/bind/xiaohongshu',
	'user/spread_image',
	'user/spread_list',
	'user/order/list',
	'user/cart/lst',
	'user/updatenickname',
	'user/updateavatar',
	'store/product/recommend/lst',
	'user/relation/create',
	'user/relation/delete',
	'coupon/receive',
	'user/address/lst',
	'order/check',
	'user/relation/batch/create',
	'user/cart/delete',
	'order/group_order_list',
	'order/list',
	'order/number',
	'order/create',
	'refund/list',
	'coupon/list',
	'user/cart/again',
	'user/relation/product/lst',
	'user/withdraw',
	'user/authbean',
	'receipt/number',
	'user/authVip'
];

const post = (url, data, callback, complete) => {
	let header = {
		'Accept': 'application/json',
		'Content-Type': 'application/json',
	}
	let urlId = [
		'coupon/receive/',
		'user/address/delete/',
		'user/address/update/',
		'user/cart/change/',
		'order/pay/',
		'order/computed/',
		'order/cancel/',
		'refund/apply/',
		'order/take/',
		'order/express/',
		'reply/',
		'refund/del/'
	]
	if(urlId.indexOf(url) >= 0) {
		url = url + data.id
		let userToken = db.get("userToken");
		header[TOKENNAME] = 'Bearer ' + userToken.token;
	}
	// 判断token是否存在
	if (methodsToken.indexOf(url) >= 0) {
		// 获取用户token
		let userToken = db.get("userToken");
		if (!userToken) {
			common.jumpToLogin();
			return false;
		} else {
			header[TOKENNAME] = 'Bearer ' + userToken.token;
		}
	}

	uni.request({
		url: apiBaseUrl + 'api/' + url,
		data: data,
		header: header,
		method: 'POST',
		success: (response) => {
			const result = response.data
			if (result.status === 200 || result.status === 400) {
				callback(response.data);
			} else if ([410000, 410001, 410002, 40000].indexOf(result.status) !== -1) {
				db.del("userToken");
				common.jumpToLogin();
				return false;
			} else if (result.status == 501) {
				uni.showToast({
					title: res.data.message,
					icon: 'none',
					duration: 1000,
					complete: function() {
						setTimeout(function() {
							uni.hideToast();
						}, 1000);
					}
				});
			} else {
				console.log(response);
			}
		},
		complete: (response) => {
			complete ? complete() : "";
		},
		fail: (error) => {
			if (error && error.message) {
				showError(error.response);
			} else {

			}
		},
	});

}


const get = (url, callback, data = '') => {

	let header = {
		'Accept': 'application/json',
		'Content-Type': 'application/json',
	}
	let urlId = [
		'order/status/',
		'order/detail/',
		'order/group_o',
		'refund/produc',
		'refund/detail',
		'user/address/',
		'article/detai',
		'coupon/store/',
		'reply/product',
		'user/merchant'
	]
	if(urlId.indexOf(url.slice(0,13)) >= 0) {
		let userToken = db.get("userToken");
		header[TOKENNAME] = 'Bearer ' + userToken.token;
	}
	// 判断token是否存在
	if (methodsToken.indexOf(url) >= 0) {
		// 获取用户token
		let userToken = db.get("userToken");
		if (!userToken) {
			common.jumpToLogin();
			return false;
		} else {
			header[TOKENNAME] = 'Bearer ' + userToken.token;
		}
	}
	if (data) {
		if (Object.keys(data).length) {
			url = common.builderUrlParams(url, data)
		}
	}
	uni.request({
		url: apiBaseUrl + 'api/' + url,
		header: header,
		method: 'GET',
		success: (response) => {
			const result = response.data
			if (result.status === 200 || result.status === 400) {
				callback(response.data);
			} else if ([410000, 410001, 410002, 40000].indexOf(result.status) !== -1) {
				db.del("userToken");
				common.jumpToLogin();
				return false;
			} else if (result.status == 501) {
				uni.showToast({
					title: res.data.message,
					icon: 'none',
					duration: 1000,
					complete: function() {
						setTimeout(function() {
							uni.hideToast();
						}, 1000);
					}
				});
			} else {
				console.log(response);
			}
		},
		fail: (error) => {
			if (error && error.response) {
				showError(error.response);
			}
		},
		complete: () => {}
	});
}

// 上传图片
export const uploadImage = (num, callback) => {
	uni.chooseImage({
		count: num,
		success: async (res) => {
			uni.showLoading({
				title: '上传中...'
			});
			let tempFilePaths = res.tempFilePaths
			for (var i = 0; i < tempFilePaths.length; i++) {
				let img = '';
				 img = await common.compress(tempFilePaths[i]);// await--上传图片之前先压缩，否则上传会比压缩先执行
				 
				uni.uploadFile({
					url: apiBaseUrl + 'api/' + 'upload/image/file',
					filePath: img,
					fileType: 'image',
					name: 'file',
					headers: {
						'Accept': 'application/json',
						'Content-Type': 'multipart/form-data',
					},
					formData: {
						'method': 'images.upload',
						'upfile': tempFilePaths[i]
					},
					success: (uploadFileRes) => {
						callback(JSON.parse(uploadFileRes.data));
					},
					fail: (error) => {
						if (error && error.response) {
							showError(error.response);
						}
					},
					complete: () => {
						setTimeout(function() {
							uni.hideLoading();
						}, 250);
					},
				});
			}
		}
	});
}
// 获取汽车,生活服务
export const getCarList = (id, data,callback) => get('user/merchant/lst/' + id, callback,data);

// 修改头像
export const setAvatar = (data,callback) => post('user/updateavatar',data,callback);

//修改昵称
export const setNickname = (data,callback) => post('user/updatenickname',data,callback);

// 获取商品详情
export const getGoodDetail = (id, callback) => get('store/product/detail/' + id, callback);

// 获取必买爆款
export const getProductBest = (data,callback) => get('store/product/hot/best', callback,data);

// 获取热销榜单
export const getProductHot = (data,callback) => get('store/product/hot/hot', callback,data);

// 获取新品推荐
export const getProductNew = (data,callback) => get('store/product/hot/new', callback,data);

// 领取会员福利套餐
export const getExclusiveSet = (data, callback) => get('user/combination/give', callback, data);

// 领取会员福利
export const getExclusive = (data, callback) => post('user/task/combination/receive', data, callback);

// 获取会员福利
export const getExclusiveList = (data, callback) => get('task/combination/give', callback, data);

// 获取我的车币提现列表
export const getHaibiList = (callback) => get('user/extract', callback);

// 审核任务
export const auditTask = (data, callback) => post('user/task/examine', data, callback);

// 获取我的余额
export const getUserBalance = (callback) => get('user/balance', callback);

// 提现规则
export const getWithdraw = (callback) => get('user/withdraw', callback);

// 获取我的车币
export const getUserBean = (callback) => get('user/bean', callback);

// 获取我的车豆
export const getUserCoin = (callback) => get('user/coin', callback);

// 获取我的贡献值
export const getUserContribution = (data, callback) => get('user/contribution', callback, data);

// 获取攻略
export const getStrategy = (id, data,callback) => get('article/lst/' + id, callback,data);

// 获取攻略详情
export const getStrategDetail = (data, callback) => post('user/course', data, callback);

// 获取发布任务套餐
export const getSetMeal = (data, callback) => get('task/combination', callback, data);

// 发布任务
export const submitPayment = (data, callback) => post('task/create', data, callback);

// 获取任务列表
export const getTaskList = (data, callback) => get('task/lst', callback, data);

// 获取我的发布列表
export const getPostList = (data, callback) => get('user/task/post', callback, data);

// 获取我的接收任务列表
export const getUserTaskList = (data, callback) => get('user/task/lst', callback, data);

// 获取我的任务详情
export const getUserTaskDetail = (id, callback) => get('task/status/detail/' + id, callback);

// 获取我的发布任务详情
export const getPostTaskDetail = (id, callback) => get('task/detail/' + id, callback);

// 领取任务
export const getTask = (data, callback) => post('user/task/receipt', data, callback);

// 提交审核
export const postTask = (data, callback) => post('user/task/submit', data, callback);

// 任务支付
export const balancePayment = (data, callback) => post('user/task/pay', data, callback);

// 会员微信支付
export const rechargeVip = (data, callback) => post('user/vip', data, callback);

// 充值微信支付
export const recharge = (data, callback) => post('user/recharge', data, callback);




// 获取版本号
export const getAppVersion = (data, callback) => post('getAppVersion', data, callback);

// 用户注册
export const reg = (data, callback) => post('user.reg', data, callback);

// 用户登录
export const login = (data, callback) => post('auth/login', data, callback);

// 用户退出
export const getLogout = (data, callback) => post('logout', data, callback);

// 获取验证码
export const registerVerify = (data, callback) => post('auth/verify', data, callback);

// 修改密码
export const registerReset = (data, callback) => post('reset', data, callback);

// 注册账号
export const register = (data, callback) => post('register', data, callback);

// 获取用户信息
export const getUserInfo = (callback) => get('user', callback);

// 提现申请
export const extractCash = (data, callback) => post('user/extract/create', data, callback);

// 提现银行/提现最低金额
export const extractBank = (callback) => get('user/extract/banklst', callback);

// 佣金数据
export const spreadInfo = (callback) => get('user/spread_info', callback);


// 用户绑定抖音
export const bingDouyin = (data, callback) => post('user/bind/douyin', data, callback);

// 用户绑定快手
export const bingKuaishou = (data, callback) => post('user/bind/kuaishou', data, callback);

// 用户绑定快手
export const bingXiaohongshu = (data, callback) => post('user/bind/xiaohongshu', data, callback);

// 文章列表
export const articleList = (cid, data, callback) => get('article/lst/' + cid, callback, data);

// 文章详情
export const articleInfo = (id, callback) => get('article/detail/' + id, callback);

// 获取客服信息
export const getService = (callback) => get('kfService', callback);

// 获取分销海报图片
export const spreadBanner = (data, callback) => get('user/spread_image', callback, data);

// 获取配置
export const shopConfig = (callback) => get('config', callback);

// 获取搜索关键字获取
export const getSearchKeyword = (callback) => get('common/hot_keyword', callback);

// 获取推广用户一级和二级
export const spreadPeople = (data, callback) => get('user/spread_list', callback, data);

// 订单列表
export const getOrderList = (data, callback) => get('order/list', callback, data);

// 获取待付款订单  未支付订单
export const groupOrderList = (data,callback) => get('order/group_order_list', callback, data);

// 获取商品推荐列表
export const getCartProductHot = (data,callback) => get('store/product/recommend/lst',callback,data);

// 获取购物车列表
export const getCartList = (callback) => get('user/cart/lst', callback);

// 获取地址数据
export const getCity = (callback) => get('system/city/lst', callback);

// 保存地址
export const editAddress = (data, callback) => post('user/address/create', data, callback);

// 购车添加
export const postCartAdd = (data, callback) => post('user/cart/create', data, callback);

// 获取购物车数量
export const getCartCounts = (callback) => get('user/cart/count', callback);

// 购车增加 递减
export const changeCartNum = (data, callback) => post('user/cart/change/', data, callback);

// 删除购物车商品
export const cartDel = (data,callback) => post('user/cart/delete',data,callback);

// 添加收藏
export const collectAdd = (data,callback) => post('user/relation/create',data,callback);

// 取消收藏
export const collectDel = (data,callback) => post('user/relation/delete',data,callback);

// 收藏购物车
export const collectAll = (data,callback) => post('user/relation/batch/create',data,callback);

// 获取优惠劵
export const getCoupons = (data,callback) => get('coupon/product',callback,data);

// 获取商品优惠劵
export const getShopCoupons = (id,callback) => get('coupon/store/' + id,callback);

// 领取商品优惠劵
export const setCouponReceive = (data,callback) => post('coupon/receive/',data,callback);

// 获取购买信息
export const orderConfirm = (data,callback) => post('order/check',data,callback);

// 获取地址信息
export const getAddressList = (data,callback) => get('user/address/lst',callback,data);

// 获取地址详情
export const getAddressDetail = (id,callback) => get('user/address/detail/' + id,callback);

// 删除地址
export const delAddress = (data,callback) => post('user/address/delete/',data,callback);

// 设置默认地址地址
export const setAddressDefault = (data,callback) => post('user/address/update/',data,callback);

// 获取订单统计数据
export const orderData = (callback) => get('order/number',callback);

// 待支付订单付款
export const orderPay = (data,callback) => post('order/pay/',data,callback);

// 购买商品付款
export const orderCreate = (data,callback) => post('order/create',data,callback);

// 获取支付状态订单(成功,失败)
export const getPayOrder = (id,callback) => get('order/status/' + id,callback);

// 获取订单详情
export const getOrderDetail = (id,callback) => get('order/detail/' + id,callback);

// 获取未支付订单详情
export const groupOrderDetail = (id,callback) => get('order/group_order_detail/' + id,callback);

// 计算订单金额
export const postOrderComputed = (data,callback) => post('order/computed/',data,callback);

// 未支付订单取消
export const unOrderCancel = (data,callback) => post('order/cancel/',data,callback);

// 获取退款商品详情
export const refundProduct = (id,data,callback) => get('refund/product/' + id,callback,data);

// 获取退款理由
export const refundMessage = (callback) => get('common/refund_message',callback);

// 申请退款
export const refundApply = (data,callback) => post('refund/apply/',data,callback);

// 退款详情
export const refundDetail = (id,callback) => get('refund/detail/' + id,callback);

// 退款详情
export const refundList = (data,callback) => get('refund/list',callback,data);

// 获取优惠劵列表
export const getUserCoupons = (callback) => get('coupon/list',callback);

// 确认订单 订单收货
export const orderTake = (data,callback) => post('order/take/',data,callback);

// 订单查询物流信息
export const express = (data,callback) => post('order/express/',data,callback);

// 再次下单
export const orderAgain = (data,callback) => post('user/cart/again',data,callback);

// 获取订单产品信息
export const orderProduct = (id,callback) => get('reply/product/' + id,callback);

// 订单查询物流信息
export const orderComment = (data,callback) => post('reply/',data,callback);



// 获取商户列表
export const storeMerchantList = (data,callback) => get('store/merchant/lst',callback,data);

// 获取商铺详情
export const getStoreDetail = (id,callback) => get('store/merchant/detail/' + id,callback);

// 获取商铺商品列表
export const getStoreGoods = (id,data,callback) => get('store/merchant/product/lst/' + id,callback,data);

// 获取首页轮播图
export const getSwiperList = (callback) => get('common/banner',callback);

// 获取搜索列表
export const getProductLists = (data,callback) => get('store/product/lst',callback,data);

// 删除退款记录
export const bindDetele = (data,callback) => post('refund/del/',data,callback);

// 获取商品收藏列表
export const getCollectUserList = (data,callback) => get('user/relation/product/lst',callback,data);

// 获取会场列表
export const getOnlineVenue = (data,callback) => post('haoyiku/getOnlineVenue',data,callback);

//好衣库场景
export const getVenueDetail = (data, callback) => post('haoyiku/getVenueDetail', data, callback);

//首页分类
export const getCategory = (data,callback) => get('store/product/category',callback,data);

//获取banner图
export const getBanner = (data,callback) => get('ad/index',callback,data);

//获取抵扣商品
export const getDeductionShop = (data,callback) => get('store/product/deduction',callback,data);

//获取抵扣商品广告位
export const getAdvertising = (data,callback) => get('ad/index',callback,data);

// 是否能够升级
export const getAuthVip = (data,callback) => get('user/authVip',callback,data);

//验证今日可使用嗨豆
export const getAuthBean = (data,callback) => get('user/authbean',callback,data);

export const getUserReceipt = (data,callback) => get('receipt/number',callback,data);
